﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Visual_Configuration_GlobalConfiguration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        verificaUser();
        loadDBConfig();

    }
    protected void verificaUser()
    {
        VSDBM.Code.dto.User usuariologado = (VSDBM.Code.dto.User)HttpContext.Current.Session["usuariologado"];
        if (usuariologado.acessgroup != 1)
        {
            HttpContext.Current.Session["exception"] = "You are not authorized!";
            Response.Redirect("~/Visual/Errors/error.aspx");
        }
    }
    protected void btnCheckDB_Click(object sender, EventArgs e)
    {
        txtServer.ReadOnly = true;
        txtPort.ReadOnly = true;
        txtUser.ReadOnly = true;
        txtPassword.ReadOnly = true;
        imgCancel.Visible = false;
        imgOk.Visible = false;
        String ConnString = ("SERVER=" + txtServer.Text + "; DATABASE=vsdbm; UID=" + txtUser.Text + "; PASSWORD=" + txtPassword.Text + ";");

        try
        {
            MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(ConnString);
            connection.Open();
            connection.Close();
        }
        catch (Exception)
        {
            txtServer.ReadOnly = false;
            txtPort.ReadOnly = false;
            txtUser.ReadOnly = false;
            txtPassword.ReadOnly = false;
            txtServer.Text = "";
            txtPort.Text = "";
            txtUser.Text = "";
            txtPassword.Text = "";
            imgOk.Visible = false;
            imgCancel.Visible = true;
            return;

        }

        imgOk.Visible = true;
        btnSaveDB.Visible = true;
    }

    protected void loadDBConfig()
    {
        VSDBM.Code.DataBaseConfig dbconf = new VSDBM.Code.DataBaseConfig();
        dbconf = dbconf.getConfig();
        txtPassword.Text = dbconf.senha;
        txtServer.Text = dbconf.servidor;
        txtPort.Text = Convert.ToString(dbconf.porta);
        txtUser.Text = dbconf.user;

    }

    protected void btnSaveDB_Click(object sender, EventArgs e)
    {
        try
        {
            VSDBM.Code.DataBaseConfig dbconf = new VSDBM.Code.DataBaseConfig(txtServer.Text, Convert.ToInt32(txtPort.Text), txtUser.Text, txtPassword.Text);
            dbconf.setConfig();
        }
        catch (Exception ev)
        {

            HttpContext.Current.Session["exception"] = ev.Message;
            Response.Redirect("../../../Visual/Errors/error.aspx");
        }
    }

       
}